package com.api.shihua.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shihua.util.BaseUtil;
import com.api.shihua.util.LogTool;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

@Path("/zjl")
public class ZJLAaction {
    private final static LogTool loghr = new LogTool("/log/zjl/",false);
    @GET
    @Path("/getSubInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getSubInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
            User user = HrmUserVarify.getUser(request, response);
            int userid= user.getUID();
            JSONObject jsons=new JSONObject();
            RecordSet re=new  RecordSet();
            String requestid=Util.null2String(request.getParameter("requestid")).trim().replaceAll(",,",",") ;
            String subid=Util.null2String(request.getParameter("subid")).trim().replaceAll(",,",",") ;
            String mainrequestid=Util.null2String(request.getParameter("mainrequestid")).trim().replaceAll(",,",",") ;
            loghr.writeLog("mainrequestid-->"+mainrequestid+"subid-->"+subid);
            if("".equals(subid)){
                return JSONArray.toJSONString(jsons);
            }
            if(subid.endsWith(",")){
                subid=subid.substring(0,subid.length()-1);
            }
            String newSub=BaseUtil.getMax_1(userid+"",subid);
            if("".equals(newSub)){
                return JSONArray.toJSONString(jsons);
            }
            if(newSub.startsWith(",")){
                newSub=newSub.substring(1);
            }
            //初始化数据
            jsons=BaseUtil.getSubData(newSub,mainrequestid+"", requestid);
            return JSONObject.toJSONString(jsons);

    }
    @GET
    @Path("/getDepInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getDepInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
        User user = HrmUserVarify.getUser(request, response);
        int userid= user.getUID();
        JSONObject jsons=new JSONObject();
        RecordSet re=new  RecordSet();
        String subid=Util.null2String(request.getParameter("depid")).trim().replaceAll(",,",",") ;
        String mainrequestid=Util.null2String(request.getParameter("mainrequestid")).trim().replaceAll(",,",",") ;
        loghr.writeLog("mainrequestid-->"+mainrequestid+"getDepInfo-->"+subid);
        if("".equals(subid)){
            return JSONArray.toJSONString(jsons);
        }
        if(subid.endsWith(",")){
            subid=subid.substring(0,subid.length()-1);
        }
        String newSub=BaseUtil.getMax_1ByDep(userid+"",subid);
        if("".equals(newSub)){
            return JSONArray.toJSONString(jsons);
        }
        if(subid.startsWith(",")){
            subid=subid.substring(1);
        }
        //初始化数据
        jsons=BaseUtil.getSubData(subid,mainrequestid+"","");
        return JSONObject.toJSONString(jsons);

    }
    @GET
    @Path("/getDepInfoLeve2")
    @Produces({MediaType.TEXT_PLAIN})
    public String getDepInfoLeve2(@Context HttpServletRequest request, @Context HttpServletResponse response){
        User user = HrmUserVarify.getUser(request, response);
        int userid= user.getUID();
        JSONObject jsons=new JSONObject();
        RecordSet re=new  RecordSet();
        String requestid=Util.null2String(request.getParameter("requestid")).trim().replaceAll(",,",",") ;
        String depid=Util.null2String(request.getParameter("depid")).trim().replaceAll(",,",",") ;
        String mainrequestid=Util.null2String(request.getParameter("mainrequestid")).trim().replaceAll(",,",",") ;
        loghr.writeLog("mainrequestid-->"+mainrequestid+"getDepInfoLeve2-->"+depid);
        if("".equals(depid)){
            return JSONArray.toJSONString(jsons);
        }
        if(depid.endsWith(",")){
            depid=depid.substring(0,depid.length()-1);
        }
        JSONObject newSub=BaseUtil.getMax_2ByUser(userid+"",depid);
        String newdep=newSub.getString("dep");
        String newdepSub=newSub.getString("sub");
        if(newSub==null||newSub.size()<=0||"".equals(newdepSub)||"".equals(newdep)){
            return JSONArray.toJSONString(jsons);
        }

        //初始化数据
        jsons=BaseUtil.getDepData(newdep,newdepSub,mainrequestid+"");
        return JSONObject.toJSONString(jsons);

    }
    @GET
    @Path("/getTopDepByUserid")
    @Produces({MediaType.TEXT_PLAIN})
    public String getDepTop(@Context HttpServletRequest request, @Context HttpServletResponse response){
        JSONObject jsons=new JSONObject();
        String userid=Util.null2String(request.getParameter("userid")).trim();
        RecordSet re=new RecordSet();
        int len=20;//最多20层
        if("".equals(userid)){
            return JSONObject.toJSONString(jsons);
        }
        User user=new User(Util.getIntValue(userid,0));
        if(user==null||Util.getIntValue(user.getUID()+"",0)==0){
            return JSONObject.toJSONString(jsons);
        }

        int depid=user.getUserDepartment();
        while(len>0){
            re.execute("select a.id,a.supdepid,a.departmentname,b.id subid,b.subcompanyname from hrmdepartment a left join hrmsubcompany b on a.subcompanyid1=b.id where a.id="+depid);
            if(re.next()){
                int supdepid=Util.getIntValue(re.getString("supdepid"),0);
                String departmentname=Util.null2String(re.getString("departmentname")).trim();
                int subid=Util.getIntValue(re.getString("subid"),0);
                String subcompanyname=Util.null2String(re.getString("subcompanyname")).trim();
                if(supdepid==0){
                    jsons.put("id",depid);
                    jsons.put("departmentname",departmentname);
                    jsons.put("subid",subid);
                    jsons.put("subcompanyname",subcompanyname);

                    break;
                }else{
                    depid=supdepid;
                }
            }
            len--;
        }
        return JSONObject.toJSONString(jsons);
    }
    @GET
    @Path("/checkDepByDep")
    @Produces({MediaType.TEXT_PLAIN})
    public String checkDepByDep(@Context HttpServletRequest request, @Context HttpServletResponse response){
        JSONObject jsons=new JSONObject();
        boolean flag=true;
        String mess="";
        String userid=Util.null2String(request.getParameter("userid")).trim();
        int depid=Util.getIntValue(Util.null2String(request.getParameter("depid")).trim(),-1) ;
        if(!"".equals(userid)&&depid>0){
            String[] userids=userid.split(",");
            for(String str:userids){
                str=Util.null2String(str).trim();
                int id=Util.getIntValue(str,-1);
                if(id<0){
                    continue;
                }
                User user=new User(id);
                int Departmentid=user.getUserDepartment();
                if(depid==Departmentid){
                    continue;
                }
                if(!ForDep(Departmentid,depid)){
                    mess=user.getLastname()+"所在部门已经超出单位分解所在科室的范围";
                    flag=false;
                    break;
                }
            }
        }
        jsons.put("flag",flag);
        jsons.put("mess",mess);
        return JSONObject.toJSONString(jsons);
    }
        public boolean ForDep(int userdep,int depid){
            boolean flag=false;
            if(userdep<=0||depid<=0||(userdep==depid)){
                return true;
            }
            RecordSet re=new RecordSet();
            int len=10;
            while(len>0){
                len--;
                re.execute("select * from hrmdepartment where id="+userdep);
                if(re.next()){
                    int supid=Util.getIntValue(re.getString("supdepid"),-1);
                    if(supid<0){
                        break;
                    }
                    if(depid==supid){
                        flag=true;
                        break;
                    }else{
                        userdep=supid;
                    }
                }
            }
            return flag;
        }
    }
